﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ToDo
{
    public partial class Form1 : Form
    {
        private List<CheckBox> _tasksBoxList = new List<CheckBox>();
        private const String DEFAULT_TEXT = "添加任务";
        private int _undoCount = 0;

        public Form1()
        {
            
            InitializeComponent();
            SetDefaultText();
            //得到焦点时
            FillTask.GotFocus += new EventHandler(text_Enter);
            //失去焦点时
            FillTask.LostFocus += new EventHandler(text_Leave);
            MenuItemNotDone.Checked = true;

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DirectoryInfo dir = new DirectoryInfo("E:/");
            FileInfo[] fil = dir.GetFiles();
            DirectoryInfo[] dii = dir.GetDirectories();
            foreach (FileInfo f in fil)
            {
                string name =  Path.GetFileName(f.FullName.ToString());
                //Console.WriteLine(name);
                if (name.Contains("todo.txt")) 
                {
                    //创建一个文件流对象，用于读文件
                    FileStream fs = new FileStream("E:/todo.txt", FileMode.OpenOrCreate, FileAccess.Read);
                    //创建一个与文件流对象相对应的二进制读入流对象
                    BinaryReader br = new BinaryReader(fs);

                    _undoCount = br.ReadInt32();
                    int count = br.ReadInt32();

                    for (int i = 0; i < count; i++)
                    {
                        CheckBox checkbox = new CheckBox();
                        checkbox.Name = br.ReadString();
                        checkbox.Text = br.ReadString();
                        checkbox.Checked = br.ReadBoolean();
                        checkbox.Font = new Font("微软雅黑", 10);
                        checkbox.BackColor = Color.Gray;
                        checkbox.Size = new Size(250, 23);
                        _tasksBoxList.Add(checkbox);
                    }

                    //关闭所有文件流对象
                    br.Close();
                    fs.Close();
                }
            }
            NowDate.Text = DateTime.Now.ToLongDateString().ToString();
            int k = 0;
            panel1.Controls.Clear();
            for (int i = 0; i < _tasksBoxList.Count; i++)
            {
                if (_tasksBoxList[i].Checked == false)
                {
                    _tasksBoxList[i].Location = new Point(10, 25 * ++k);
                    panel1.Controls.Add(_tasksBoxList[i]);
                }
            }
            MenuItemNotDone.Checked = true;
            MenuItemHaveDone.Checked = false;
            Invalidate();
        }

        //TextBox默认显示文字
        private void SetDefaultText()
        {
             //默认文本
            FillTask.Text = DEFAULT_TEXT;
             //默认文本颜色为灰色
            FillTask.ForeColor = Color.Gray;         
        }
        //获取焦点事件 Enter
        private void text_Enter(object sender, EventArgs e)
        {
             if (FillTask.Text == DEFAULT_TEXT)
             {
                //清空文本框
                FillTask.Text = "";
                FillTask.ForeColor = Color.Black;
             }

        }
        //失去焦点事件 Leave
        private void text_Leave(object sender, EventArgs e)
        {
            //如果文本框为空，则显示默认文本
            if (String.IsNullOrEmpty(FillTask.Text))
            {
                //默认文本
                SetDefaultText();
            }

        }

        //回车键添加事项
        private void FillTask_KeyDown(object sender, KeyEventArgs e)
        {
            //按下回车键添加任务
            if (e.KeyValue == 13 && FillTask.Text != "" && FillTask.Focused)
            {
                _undoCount++;
                CheckBox checkbox = new CheckBox();
                checkbox.Name = "taskCheckBox" + _undoCount;
                checkbox.Text = FillTask.Text; ;
                checkbox.Font = new Font("微软雅黑", 10);
                checkbox.Checked = false;
                checkbox.BackColor = Color.Gray;
                checkbox.Size = new Size(250, 23);
                checkbox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.CheckBox_MouseDown);
                _tasksBoxList.Add(checkbox);
                if (MenuItemNotDone.Checked == true)
                {
                    int cnt = 0;
                    for (int i = 0; i < _tasksBoxList.Count; i++)
                        if (_tasksBoxList[i].Checked == false)
                            cnt++;
                    checkbox.Location = new Point(10, 25 * cnt);
                    panel1.Controls.Add(checkbox);

                }
                FillTask.Text = "";
            }
            Invalidate();
        }

        //鼠标添加事项
        private void buttonAddTask_Click(object sender, EventArgs e)
        {
            if (FillTask.Text != "" && FillTask.Text != "添加任务")
            {
                _undoCount++;
                CheckBox checkbox = new CheckBox();
                checkbox.Name = "taskCheckBox" + _undoCount;
                checkbox.Text = FillTask.Text; ;
                checkbox.Font = new Font("微软雅黑", 10);
                checkbox.Checked = false;
                checkbox.BackColor = Color.Gray;
                checkbox.Size = new Size(250, 23);
                checkbox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.CheckBox_MouseDown);
                _tasksBoxList.Add(checkbox);
                if (MenuItemNotDone.Checked == true)
                {
                    int cnt = 0;
                    for (int i = 0; i < _tasksBoxList.Count; i++)
                        if (_tasksBoxList[i].Checked == false)
                            cnt++;
                    checkbox.Location = new Point(10, 25 * cnt);
                    panel1.Controls.Add(checkbox);

                }
                FillTask.Text = "";
            }
            Invalidate();
        }

        //显示未完成事项
        private void MenuItemNotDone_Click(object sender, EventArgs e)
        {
            int k = 0;
            panel1.Controls.Clear();
            for (int i = 0; i < _tasksBoxList.Count; i++)
            {
                if (_tasksBoxList[i].Checked == false)
                {
                    _tasksBoxList[i].Location = new Point(10, 25 * ++k);
                    panel1.Controls.Add(_tasksBoxList[i]);
                }
            }
            MenuItemNotDone.Checked = true;
            MenuItemHaveDone.Checked = false;
            Invalidate();
        }

        //显示已完成事项
        private void MenuItemHaveDone_Click(object sender, EventArgs e)
        {
            int k = 0;
            panel1.Controls.Clear();
            for (int i = 0; i < _tasksBoxList.Count; i++)
            {
                if (_tasksBoxList[i].Checked == true)
                {
                    _tasksBoxList[i].Location = new Point(10, 25 * ++k);
                    panel1.Controls.Add(_tasksBoxList[i]);
                }
            }
            MenuItemNotDone.Checked = false;
            MenuItemHaveDone.Checked = true;
            Invalidate();
        }

        //对每条事项的鼠标操作
        private void CheckBox_MouseDown(object sender, MouseEventArgs e)
        {
            CheckBox checkbox = (CheckBox)sender;
            //鼠标左键选择是否完成
            if (e.Button == MouseButtons.Left)
            {
                if (checkbox.Checked == true)
                    checkbox.Checked = false;
                else
                    checkbox.Checked = true;
                
            }
            else if (e.Button == MouseButtons.Right)
            {
                for (int i = 0; i < _tasksBoxList.Count; i++)
                {
                    if (_tasksBoxList[i].Name.Equals(checkbox.Name))
                    {
                        _tasksBoxList.RemoveAt(i);
                        break;
                    }
                }
            }
            int q = 0;
            panel1.Controls.Clear();
            if (MenuItemNotDone.Checked == true)
            {
                for (int i = 0; i < _tasksBoxList.Count; i++)
                {
                    if (_tasksBoxList[i].Checked == false)
                    {
                        _tasksBoxList[i].Location = new Point(10, 25 * ++q);
                        panel1.Controls.Add(_tasksBoxList[i]);
                    }
                }
            }
            else if (MenuItemHaveDone.Checked == true)
            {
                for (int i = 0; i < _tasksBoxList.Count; i++)
                {
                    if (_tasksBoxList[i].Checked == true)
                    {
                        _tasksBoxList[i].Location = new Point(10, 25 * ++q);
                        panel1.Controls.Add(_tasksBoxList[i]);
                    }
                }
            }
            Invalidate();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }

        //关闭前保存文件
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            //创建一个文件流对象，用于写文件(需要添加System.IO命名空间)
            FileStream fs = new FileStream("E:\\todo.txt", FileMode.Create);
            //创建一个与文件流对象相对应的二进制写入流对象
            BinaryWriter bw = new BinaryWriter(fs);


            bw.Write(_undoCount);
            bw.Write(_tasksBoxList.Count);
            for (int i = 0; i < _tasksBoxList.Count; i++)
            {
                bw.Write(_tasksBoxList[i].Name);
                bw.Write(_tasksBoxList[i].Text);
                bw.Write(_tasksBoxList[i].Checked);
            }

            //关闭所有文件流对象
            bw.Close();
            fs.Close();
        }

        //清空已完成事项
        private void clearMenuItemHavedone_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < _tasksBoxList.Count; )
            {
                if (_tasksBoxList[i].Checked == true)
                    _tasksBoxList.RemoveAt(i);
                else
                    i++;
            }
            int k = 0;
            panel1.Controls.Clear();
            if (MenuItemNotDone.Checked == true)
            {
                for (int i = 0; i < _tasksBoxList.Count; i++)
                {
                    if (_tasksBoxList[i].Checked == false)
                    {
                        _tasksBoxList[i].Location = new Point(10, 25 * ++k);
                        panel1.Controls.Add(_tasksBoxList[i]);
                    }
                }
            }
            else if (MenuItemHaveDone.Checked == true)
            {
                for (int i = 0; i < _tasksBoxList.Count; i++)
                {
                    if (_tasksBoxList[i].Checked == true)
                    {
                        _tasksBoxList[i].Location = new Point(10, 25 * ++k);
                        panel1.Controls.Add(_tasksBoxList[i]);
                    }
                }
            }
            Invalidate();
        }

        //清空未完成事项
        private void clearMenuItemNotdo_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < _tasksBoxList.Count;)
            {
                if (_tasksBoxList[i].Checked == false)
                    _tasksBoxList.RemoveAt(i);
                else
                    i++;
            }
            int k = 0;
            panel1.Controls.Clear();
            if (MenuItemNotDone.Checked == true)
            {
                for (int i = 0; i < _tasksBoxList.Count; i++)
                {
                    if (_tasksBoxList[i].Checked == false)
                    {
                        _tasksBoxList[i].Location = new Point(10, 25 * ++k);
                        panel1.Controls.Add(_tasksBoxList[i]);
                    }
                }
            }
            else if (MenuItemHaveDone.Checked == true)
            {
                for (int i = 0; i < _tasksBoxList.Count; i++)
                {
                    if (_tasksBoxList[i].Checked == true)
                    {
                        _tasksBoxList[i].Location = new Point(10, 25 * ++k);
                        panel1.Controls.Add(_tasksBoxList[i]);
                    }
                }
            }
            Invalidate();
        }

        //清空所有事项
        private void clearMenuItemAll_Click(object sender, EventArgs e)
        {
            _tasksBoxList.Clear();
            panel1.Controls.Clear();
            Invalidate();
        }
    }
}
